$package('upms.user');
upms.user = function(){
	var _box = null;
	var _box2 = null;
	var _this = {
		addAction:baseUrl+'/user/add'+'?applicationId='+applicationId,
		editAction:baseUrl+'/user/edit'+'?applicationId='+applicationId,
		deleteAction:baseUrl+'/user/delete'+'?applicationId='+applicationId,
		allotRoleAction:baseUrl+'/user/allotRole'+'?applicationId='+applicationId,
		queryUserRoleAction:baseUrl+'/user/queryUserRole',
		editForm:function(){
			return $("#editForm");
		},
		editWin:function(){
			return $("#edit-win");
		},
		allotRoleForm:function(){
			return $("#allotRoleForm");
		},
		allotRoleWin:function(){
			return $("#allotRole-win");
		},
		add:function(){
			if(_this.editForm().form('validate')){
				_this.editForm().attr('action',_this.addAction);
				upms.saveForm(_this.editForm(),function(data){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.editWin().dialog('close');
				});
			}
		},
		edit:function(){
			if(_this.editForm().form('validate')){
				_this.editForm().attr('action',_this.editAction);
				upms.saveForm(_this.editForm(),function(data){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.editWin().dialog('close');
				});
			}
		},
	    deleteData:function(){
			var rows = $('#data-list').datagrid('getChecked');
			if (upms.checkSelect(rows)){
					$.messager.confirm('Confirm','是否删除选中记录?',function(r){  
						if (r){
					    	var arr = [],idKey = 'userId'; //主键名称
					    	$.each(rows,function(i,record){
					    		arr.push('userIds='+record[idKey]);
					    	});
					    	var data = arr.join("&");
					    	upms.deleteForm(_this.deleteAction,data,function(){
								var param = $("#searchForm").serializeObject();
								$("#data-list").datagrid('reload',param);
							});
						}  
				});
			}
		},
		allotRole:function(){
			var rows = $('#role-data-list').datagrid('getChecked');
			if (upms.checkSelect(rows)){
				var userId = _this.allotRoleForm().find("input[name=userId]").val();
				var applicationId=_this.allotRoleForm().find("input[name=applicationId]").val();
		    	var arr = [],idKey = 'roleId'; //主键名称
		    	arr.push("userId="+userId);
		    	arr.push("applicationId="+applicationId);
		    	$.each(rows,function(i,record){
		    		arr.push('roleIds='+record[idKey]);
		    	});
		    	var data = arr.join("&");
		    	upms.deleteForm(_this.allotRoleAction,data,function(){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.allotRoleWin().dialog('close');
				});
			}
		},
		initForm:function(){
			//新增操作
			$("#addBtn").click(function(){
				$("#editForm").resetForm();
				$("input[name=userName]").removeAttr("readonly");
				$("#edit-win").dialog({
					buttons:[
						{
							text:'保  存',
							iconCls: 'icon-ok',
							handler:function(){
								_this.add();
							}
						},
						{
							text:'关  闭',
							iconCls: 'icon-cancel',
							handler:function(){
								_this.editWin().dialog('close');
							}
						}
				]});
				_this.editWin().dialog('open');
			});
			//删除操作
			$("#deleteBtn").click(function(){
				_this.deleteData();
			});
			
			//修改操作
			$("#editBtn").click(function(){
				$("#editForm").resetForm();
				//只能对一条数据编辑
				var rows = $('#data-list').datagrid('getChecked');
				if(rows.length > 1){
					upms.alert('警告','请单选记录后，再进行操作.','warning');
					return false;
				}
				var row = $('#data-list').datagrid('getSelected');
				if (row){
					var data ={};
					var idKey = 'userId'; //主键名称
 					data[idKey] = row.userId;
					upms.getById(baseUrl+"/user/getById",data,function(result){
						$("#editForm").form('load',result);
						$("input[name=userName]").attr("readonly","readonly");
						$("input[name=password]").val("");
						$("#edit-win").dialog({
	   						buttons:[
	   							{
	   								text:'保  存',
	   								iconCls: 'icon-ok',
	   								handler:function(){
	   									_this.edit();
	   								}
	   							},
	   							{
	   								text:'关  闭',
	   								iconCls: 'icon-cancel',
	   								handler:function(){
	   									_this.editWin().dialog('close');
	   								}
	   							}
	   					]});
						_this.editWin().dialog('open');
	   				});
				}else{
					upms.alert('警告','未选中记录.','warning');
				}
			});
			//分配角色操作
			$("#allotRoleBtn").click(function(){
				//只能对一条数据编辑
				var rows = $('#data-list').datagrid('getChecked');
				if(rows.length > 1){
					upms.alert('警告','请单选记录后，再进行操作.','warning');
					return false;
				}
				var row = $('#data-list').datagrid('getSelected');
				if (row){
					var data ={};
					var idKey = 'userId'; //主键名称
 					data[idKey] = row.userId;
 					data["applicationId"]=applicationId;
					upms.getById(_this.queryUserRoleAction,data,function(result){
						_this.allotRoleForm().form('load',result);
						_this.allotRoleForm().find("input[name=userId]").val(row.userId);
						_this.allotRoleForm().find("input[name=applicationId]").val(applicationId);
						$('#role-data-list').datagrid('unselectAll');
						$('#role-data-list').datagrid('uncheckAll');
						//选择一行
						var rows = $('#role-data-list').datagrid('getRows');
						$.each(rows,function(index,row){
							for(var i=0;i<result.length;i++){
								if(row.roleId==result[i].roleId){
									$('#role-data-list').datagrid('checkRow',index);
								}
							}
						})
						_this.allotRoleWin().dialog({
	   						buttons:[
	   							{
	   								text:'保  存',
	   								iconCls: 'icon-ok',
	   								handler:function(){
	   									_this.allotRole();
	   								}
	   							},
	   							{
	   								text:'关  闭',
	   								iconCls: 'icon-cancel',
	   								handler:function(){
	   									_this.allotRoleWin().dialog('close');
	   								}
	   							}
	   					]});
						_this.allotRoleWin().dialog('open');
	   				});
				}else{
					upms.alert('警告','未选中记录.','warning');
				}
			});
		},
		config:{
  			dataGrid:{
  				title:'用户列表',
	   			url:'dataList?applicationId='+applicationId,
	   			loadMsg:"处理中，请稍候…",
			    toolbar:"#toolbar",
			    idField:"userId",
	   			columns:[[
						{field:'userId',checkbox:true},
						{field:'userName',title:'用户名',width:120,align:'center',sortable:true},
						{field:'nickName',title:'昵称',width:80,align:'center',sortable:true},
						{field:'locked',title:'状态',width:80,align:'center',sortable:true,
						formatter:function(value,row,index){
							if(value == 0){
								return "可用";
							}
							if(value == 1){
								return "禁用";
							}
						}},
						{field:'createTime',title:'创建时间',width:120,align:'center',sortable:true,formatter:upms.dateTimeFormatter},
						{field:'lastLoginTime',title:'最近登录时间',width:120,align:'center',sortable:true,formatter:upms.dateTimeFormatter}
				]]
			}
		},
		roleConfig:{
  			dataGrid:{
	   			url:baseUrl+'/role/dataList?applicationId='+applicationId,
	   			loadMsg:"处理中，请稍候…",
			    idField:"roleId",
			    pagination:false,
			    fitColumns:true,
			    columns:[[
					{field:'roleId',checkbox:true},
					{field:'roleName',title:'角色名称',width:120,align:'center',sortable:true},
					{field:'roleKey',title:'角色KEY',width:80,align:'center',sortable:true},
					{field:'roleDesc',title:'角色描述',width:140,align:'center',sortable:true}
				]],
				onLoadSuccess: function(){
					$('#role-data-list').datagrid('unselectAll');
					$('#role-data-list').datagrid('uncheckAll');
				}
			}
		},
		init:function(){
			_this.initForm();
			_box = new MyDataGrid(_this.config); 
			_box.init();
			$('#role-data-list').datagrid(_this.roleConfig.dataGrid);
		}
	}
	_this._box = _box;
	return _this;
}();

$(function(){
	upms.user.init();
});		